<?php

	/**
	 * @copyright SilverBiology, LLC
	 * @author Michael Giddens
	 * @website http://www.silverbiology.com
	 * @package SilverCollection
	 *	@since Command available since Release 1.1.0
	 */

	session_start();
	ini_set("display_errors", 1);
	set_time_limit( 180 );
	ini_set("memory_limit","200M");
	
	$old_error_handler = set_error_handler("myErrorHandler");
	
	$expected=array(
		  'cmd'
		, 'output'
		, 'comment_id'
		, 'status'
		, 'filter'
		, 'family'
		, 'genus'
		, 'species'
		, 'limit'
		, 'start'
		, 'sort'
		, 'dir'
		, 'attributes'
		, 'browse'
		, 'characters'
		, 'search_value'
		, 'search_type'
		, 'task'
		, 'categoryID'
		, 'valueID'
		, 'value'
		, 'imageID'
		, 'nodeApi'
		, 'nodeValue'
		, 'barcode'
		, 'width'
		, 'height'
	);
	
	// Initialize allowed variables
	foreach ($expected as $formvar)
		$$formvar = (isset(${"_$_SERVER[REQUEST_METHOD]"}[$formvar])) ? ${"_$_SERVER[REQUEST_METHOD]"}[$formvar]:NULL;

	foreach ($expected as $formvar) {
		if ($$formvar == "undefined") $$formvar = "";
	}

	require_once( "../../../config.php");
	
	$sa = new SilverAdmin;
	$user_access = new Access_user;

	if(!isset($output)) {
		$output = "json";
	}

	if ( $sa->load() ) {
	
		$valid = true;
		switch( $cmd ) {
		
			/**
			 * Command for user login
			 * required arguments: username, password
			 */
			case 'loadConfig':
				include_once("../config.php"); 
				header('Content-type: application/json');
				print json_encode(array('success'=>true, 'data' => array($config) ) );
				break;
			
			case 'saveConfig':
				$config = $_REQUEST['config'];
				$output = '';
				if(is_array($config)) {
				foreach($config as $name => $value) {
					if(is_array($value)) {
					foreach($value as $name2 => $value2) {
						if(is_array($value2)) {
						foreach($value2 as $name3 => $value3) {
							if(is_array($value3)) {
							foreach($value3 as $name4 => $value4) {
								$output .= sprintf("\$config['%s']['%s']['%s']['%s'] = '%s';\r\n", $name, $name2, $name3, $name4, stripslashes($value4) );
							}
							} else {
								$output .= sprintf("\$config['%s']['%s']['%s'] = '%s';\r\n", $name, $name2, $name3, stripslashes($value3));
							}
						}
						} else {
							$output .= sprintf("\$config['%s']['%s'] = '%s';\r\n", $name, $name2, stripslashes($value2) );
						}
					}
					} else {
						$output .= sprintf("\$config['%s'] = '%s';\r\n", $name, stripslashes($value) );
					}
				}
				}
				$output = "<?php\r\n" . $output . "?>";
				file_put_contents("../testConfig.php", $output);
				header('Content-type: application/json');
				print json_encode(array('success'=>true) );
				break;

			default:
				print_c( json_encode( array( 'success' => false,  'error' => array( 'code' => 105 ,'msg' => $sa->error(105) ) ) ) );
				break;

		}

	} else {
		print_c( json_encode( array( 'success' => false, 'error' => 'Error in Connection' ) ) );
	}


	/**
	 * Function print_c (Print Callback)
	 * This is a wrapper function for print that will place the callback around the output statement
	 */
	function print_c( $str ) {
		header('Content-type: application/json');
		if ( isset( $_REQUEST['callback'] ) ) {
			$cb = $_REQUEST['callback'] . '(' . $str . ')';
		} else {
			$cb = $str;
		}
		print $cb;
	}

	/**
	 * Function myErrorHandler
	 * Used to catch any errors and send back a custom json error message.
	 */
	function myErrorHandler($errno, $errstr, $errfile, $errline) {
	
		switch ($errno) {
		
			case E_USER_ERROR:
				
				$msg = "ERROR [$errno] $errstr<br />";
				$msg .= "  Fatal error on line $errline in file $errfile";
				$msg .= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />";
				$msg .= "Aborting...<br />";
				print_c( json_encode( array( 'success' => false,  'error' => array('msg' => $msg , 'code' => $errno ) ) ) );
				exit(1);
				break;
/*			
			case E_USER_WARNING:
				echo "<b>WARNING</b> [$errno] $errstr<br />\n";
				break;
			
			case E_USER_NOTICE:
				echo "<b>NOTICE</b> [$errno] $errstr<br />\n";
				break;
			
			default:
				echo "Unknown error type: [$errno] $errstr<br />\n";
				break;*/
		}
		
		/* Don't execute PHP internal error handler */
		return true;
	}


?>